﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogicUniversityBiz;

namespace LogicUniversity.clerks
{
    public partial class AddDelivery : System.Web.UI.Page
    {
        private OrderControl orderControl;
        protected void Page_PreInit(Object sender, EventArgs e)
        {
            //(Session["Role"]
            if (Session["Role"] as string != "Clerk")
            {
                Response.Redirect("~/Login.aspx");
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fillData();
            }
        }

        public void fillData()
        {
            orderControl = new OrderControl();
            
            List<int> purchaseOrders = orderControl.getPurchaseOrderNoNotDelievered();

            if (purchaseOrders == null || purchaseOrders.Count <= 0)
            {
                lblMsg.Text = "Currently No Purchase To Be Recorded.";
                ddlPO.Visible = false;
                txtDate.Visible = false;
                btnSave.Visible = false;
                btnCal.Visible = false;

                lblDate.Visible = false;
                lblPO.Visible = false;
            }
            else
            {
                ddlPO.DataSource = purchaseOrders;
                ddlPO.DataBind();



                int po = Convert.ToInt32(ddlPO.Items[ddlPO.SelectedIndex].Value.ToString());
                DelieverView.DataSource = orderControl.getOrderItemsDetails(po);
                DelieverView.DataBind();
            }
        }

        protected void ddlPO_SelectedIndexChanged(object sender, EventArgs e)
        {
            orderControl=new OrderControl();
            int po = Convert.ToInt32(ddlPO.Items[ddlPO.SelectedIndex].Value.ToString());

            DelieverView.DataSource = orderControl.getOrderItemsDetails(po);
            DelieverView.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
           
            orderControl = new OrderControl();
            int PO_NO = Convert.ToInt32(ddlPO.Items[ddlPO.SelectedIndex].Value.ToString());
            DateTime deliveryDate;
            try
            {
                deliveryDate = DateTime.Parse(txtDate.Text.Trim());
            }
            catch (FormatException fe)
            {
                return;
            }

            Delivery_Order delivery = new Delivery_Order();
            delivery.PO_No = PO_NO;
            delivery.Delivery_Date = deliveryDate;

            Delivery_Detail detail;

            foreach (GridViewRow row in DelieverView.Rows)
            {
                detail = new Delivery_Detail();
                detail.Item_Code = row.Cells[0].Text;
                detail.Quantity = Convert.ToInt32(row.Cells[2].Text.Trim());
                detail.Remarks = ((TextBox)row.Cells[3].FindControl("txtRemarks")).Text;

                delivery.Delivery_Detail.Add(detail);
            }

            orderControl.addPurchaseOrderDelivery(delivery);

            fillData();
            lblMsg.Text = "Purchase Order has been recoreded";
        }

        protected void btnCal_Click(object sender, EventArgs e)
        {
            //deliveryDate.Visible = true;
        }

        //protected void deliveryDate_SelectionChanged(object sender, EventArgs e)
        //{
        //    txtDate.Text = deliveryDate.SelectedDate.ToShortDateString();
        //    deliveryDate.Visible = false;
        //}
    }
}